package me.guizhi.work.web;

import me.guizhi.work.domain.User;
import me.guizhi.work.service.UserImpl;
import me.guizhi.work.service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "LoginServlet", value = "/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        String username = request.getParameter("username");
        String pwd = request.getParameter("password");
        response.setCharacterEncoding("utf8");

        response.getWriter().println("uname:"+username);
        response.getWriter().println("pwd:"+pwd);
        UserService userService = new UserImpl();
        try {
            User user = userService.login(username,pwd);
            if (null != user) {
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                response.sendRedirect("success.html");
            } else {
                response.getWriter().println("登陆失败，检查账号密码");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
